package pwd

import (
	"giv-vue-blog/global"
	"golang.org/x/crypto/bcrypt"
)

// HashAndSalt 加密密码
func HashAndSalt(pwd string) string {
	hash, err := bcrypt.GenerateFromPassword([]byte(pwd), bcrypt.MinCost)
	if err != nil {
		global.Log.Error(err)
		return ""
	}
	return string(hash)
}

// CheckPwd 验证密码 hash之后的密码 输入的密码
func CheckPwd(hashPwd, pwd string) bool {
	byteHash := []byte(hashPwd)
	err := bcrypt.CompareHashAndPassword(byteHash, []byte(pwd))
	if err != nil {
		global.Log.Error(err)
		return false
	}
	return true
}
